package edu.ccut.saturn.studio.wfg.wfgmodel;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.wfg.utils.ModelXmlUtils;

public class SaturnParticipator {
	
	// 组织结构与角色、流程启动者、流程执行者、自定义参与者；三个字段可合并为一个字段:令其为1，2，3，4
	private String obtainFromActivityRadioButton = "1";
	
	// 从活动中获取
	private String userFrom = "";
	
	// 校验参与者规则
	private String validateUserLogic = "";
	
	private List<SaturnAttribute> userAttributeList = new ArrayList<SaturnAttribute>();
	
	private LogicKeyAttribute customDefineParticipant = new LogicKeyAttribute();

	public String getObtainFromActivityRadioButton() {
		return obtainFromActivityRadioButton;
	}

	public void setObtainFromActivityRadioButton(
			String obtainFromActivityRadioButton) {
		this.obtainFromActivityRadioButton = obtainFromActivityRadioButton;
	}

	public String getUserFrom() {
		return userFrom;
	}

	public void setUserFrom(String userFrom) {
		this.userFrom = userFrom;
	}

	public String getValidateUserLogic() {
		return validateUserLogic;
	}

	public void setValidateUserLogic(String validateUserLogic) {
		this.validateUserLogic = validateUserLogic;
	}

	public List<SaturnAttribute> getUserAttributeList() {
		return userAttributeList;
	}

	public void setUserAttributeList(List<SaturnAttribute> userAttributeList) {
		this.userAttributeList = userAttributeList;
	}
	
	Node createNode(Document document, Node node){
		SaturnW3CUtil.createChildNode(node, SaturnName.KEY_NODE_ELEMENT_OBTAINFROMACTIVITYRADIOBUTTON, this.obtainFromActivityRadioButton);
		Node participatorNode = document.createElement(SaturnName.KEY_NODE_ELEMENT_USERS);
		for (int i = 0; i < userAttributeList.size(); i++) {
			Node node1 = userAttributeList.get(i).creatNode(document, SaturnName.KEY_NODE_NAME_USER);
			participatorNode.appendChild(node1);
		}
		node.appendChild(participatorNode);
		SaturnW3CUtil.createChildNode(node, SaturnName.KEY_NODE_ELEMENT_VALIDATEUSERLOGIC, this.validateUserLogic);
		SaturnW3CUtil.createChildNode(node, SaturnName.KEY_NODE_ELEMENT_USERFROM, this.userFrom);
		
		Node customDefineParticipantNode = document.createElement(SaturnName.KEY_NODE_ELEMENT_CUSTOMDEFINEPARTICIPANT);
		
		Node fullKeyNode = document.createElement(SaturnName.KEY_NODE_NAME_KEY);
		SaturnW3CUtil.createAttribute(fullKeyNode, SaturnName.KEY_NODE_NAME_FULLKEY, this.getCustomDefineParticipant().getKey());
		createLogicKeyNodeList(document, fullKeyNode, this.customDefineParticipant.getAttributes(), SaturnName.KEY_NODE_NAME_PARAMETER);
		
		customDefineParticipantNode.appendChild(fullKeyNode);
		
		node.appendChild(customDefineParticipantNode);
		return node;
	}
	
	protected void createLogicKeyNodeList(Document document, Node node,
			List<SaturnAttribute> parameters, String nodeName){
		if(parameters.size()==0 || parameters == null){
			return;
		}
		for(SaturnAttribute parameter : parameters){
			Node parameterNode = createChildNode(document,parameter);
			node.appendChild(parameterNode);
		}
	}
	
	protected Node createChildNode(Document document,SaturnAttribute attribute) {
		return attribute.creatNode(document, null);
	}

	// 后加的
	public LogicKeyAttribute getCustomDefineParticipant() {
		return customDefineParticipant;
	}

	public void setCustomDefineParticipant(LogicKeyAttribute customDefineParticipant) {
		this.customDefineParticipant = customDefineParticipant;
	}
}
